package com.myacg.editor.common.unit.html;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 封装一些对elementblock的一些便利的操作
 * @author Administrator
 *
 */
public class ElementBlockUtils {

	/**
	 * 根据元素的属性项目来筛选对应的列表（全查）
	 * @param allElement
	 * @param attrs
	 * @return
	 */
	public static List<ElementBlock> findElementBlockByAttributes(List<ElementBlock> allElement,Map<String,String> attrs){
		  Set<String> attrNames = attrs.keySet();
		  List<ElementBlock>  findList = new ArrayList<>();
		  boolean isfind = true;
		  for (ElementBlock elementBlock : allElement) {
			  
			  for (String attrName : attrNames) {
				  String value = elementBlock.getAttrItems().getItemValue(attrName);
				  String filterValue = attrs.get(attrName);
				  //element不满足某项属性
				  if(!filterValue.equals(value)){
					  isfind = false;
				  }
			  }
			  //如果全部条件通过则把元素加入到list中
			  if(isfind){
				  findList.add(elementBlock);
			  }
		  }
		  return findList;
	}
	
	/**
	 * 根据id获取对应的element
	 * @param allElement
	 * @param id
	 * @return
	 */
	public static  ElementBlock findElementBlockByid(List<ElementBlock> allElement,String id){
		  ElementBlock   findBlock = null ;
		  for (ElementBlock elementBlock : allElement) {
				  String value = elementBlock.getAttrItems().getItemValue("id");
				  //element不满足某项属性
				  if(id.equals(value)){
					  findBlock = elementBlock;
					  break;
				  }
		  }
		  return findBlock;
	}
	
	/**
	 * 获取某个列表的name
	 * @param allcss
	 * @param name
	 * @return
	 */
	public static  CssBlock findCssBlockByName(List<CssBlock> allcss,String name){
		CssBlock findCssBlock = null;
		for (CssBlock cssBlock : allcss) {
			if(name.equals(cssBlock.getName())){
				findCssBlock = cssBlock;
				break;
			}
		}
		return findCssBlock;
	}
	
}
